#include <cstdio>
#include <cstring>

using namespace std;

char in[105] = {0};
int A[3] = {0};

bool checkPAT(){
    int number_t = 0, number_p = 0;
    for(int i = 0; i < strlen(in); i++){
        if(in[i] != 'P' && in[i] != 'A' && in[i] != 'T'){
            return false;
        }
        if(in[i] == 'P'){
            number_p++;
            A[0] = i;
        }
        if(in[i] == 'T'){
            number_t++;
            A[1] = i - A[0] - 1;
            A[2] = strlen(in) - i - 1;
        }
    }
    if(A[2] == A[1] * A[0] && A[1] != 0 && number_t == number_p == 1){
        return true;
    }
    return false;
}

int main()
{
    int N = 0;
    scanf("%d", &N);
    for(int i = 0; i < N; i++){
        scanf("%s", in);
        printf("%s\n", checkPAT()?"YES":"NO");
    }
}
